package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.w3c.dom.Text;
import org.xutils.x;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.PersonBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.Tools;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/7/25.
 */
public class PersonHomeActivity extends Activity implements View.OnClickListener,OnNetResultListener {
    private TextView back_iamge;
    private TextView title_tv;
   private PersonBean.DataBean bean;
    private CircularImage activity_personhome_icon;
    private RelativeLayout activity_personhome_remark;
    private RelativeLayout acitivity_personhome_change;
    private TextView activity_personhome_name;
    private boolean flag = false;
    private ImageView acitivity_personhome_change_iv;
    private String name;
    private TextView activity_personhome_remark_name;
    private TextView addfriend_Ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personhome);

        bean = (PersonBean.DataBean) getIntent().getSerializableExtra(AddFriendsActivity.ADDFRIENDSACTIVITY);
        initView();
        initData();

    }

    private  void  initView(){
        back_iamge = (TextView) findViewById(R.id.back_iamge);
        back_iamge.setText("添加好友");
        title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText("我的帮友");
        activity_personhome_icon = (CircularImage) findViewById(R.id.activity_personhome_icon);
        activity_personhome_remark = (RelativeLayout) findViewById(R.id.activity_personhome_remark);
        acitivity_personhome_change = (RelativeLayout) findViewById(R.id.acitivity_personhome_change);
        activity_personhome_name = (TextView) findViewById(R.id.activity_personhome_name);
        acitivity_personhome_change_iv = (ImageView) findViewById(R.id.acitivity_personhome_change_iv);
        acitivity_personhome_change_iv.setOnClickListener(this);
        activity_personhome_remark.setOnClickListener(this);
        activity_personhome_remark_name = (TextView) findViewById(R.id.activity_personhome_remark_name);
        addfriend_Ok = (TextView) findViewById(R.id.addfriend_Ok);
        addfriend_Ok.setOnClickListener(this);
    }

    private void initData(){
         name = bean.getNvc_Name();
       if ("".equals(name)){
           activity_personhome_name.setText("--");
       }else{
           activity_personhome_name.setText(name);
       }
        if(bean.getNvc_TitlePhoto()==null){
            activity_personhome_icon.setImageResource(R.mipmap.addfriends_default);
        }else{
            x.image().bind(activity_personhome_icon,""+bean.getNvc_TitlePhoto(), ImageOptionsUtil.getOptions292_700());
        }


    }
    public void back(View view){
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.acitivity_personhome_change_iv:
                if(!flag){
                    acitivity_personhome_change_iv.setImageResource(R.mipmap.switch_button_selector);
                    flag = true;
                }else{
                    acitivity_personhome_change_iv.setImageResource(R.mipmap.switch_button_normal);
                    flag = false;
                }
                break;
            case R.id.activity_personhome_remark:
                    Intent intent = new Intent(this,NameRemarkActivity.class);
                    startActivityForResult(intent,1);
                break;
            //点击加为好友
            case R.id.addfriend_Ok:
                getNetData();
                break;

        }
    }

    private CommonResultBean bean1;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof CommonResultBean){
            bean1 = (CommonResultBean) data;
            switch (bean1.getResCode()){
                case "0000":
                    CUtils.showToast("好友添加成功");
                    Intent intent = new Intent(this,FindHelp.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                    break;
                case "0003":
                    Toast.makeText(PersonHomeActivity.this,bean1.getMsg(),Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    /**
     * POST Wedding/Share_WeedingBook?Token={Token}&i_Id={i_Id}
     加好友
     */
   private void getNetData(){
      String Useurl = UrlDealUtil.getRealUrl(RequestCode.SHARE_WEEDINGBOOK);
       HashMap<String,String> paramsMap=new HashMap<>();
       paramsMap.put("CanEdit",""+flag);
       paramsMap.put("MemberRID",""+bean.getI_Id());
       paramsMap.put("Name",name);
       HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST,Useurl, this, CommonResultBean.class,RequestCode.SHARE_WEEDINGBOOK, paramsMap);
       HttpHandleUtil.executeCommonPost(this,httpRequestBean);
   }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getStringExtra("Flag") != null) {
            name =  data.getStringExtra("Flag");
            activity_personhome_remark_name.setText(name);
        }
    }

}
